package com.gzc.just.play.last.war.common.serializer;

/**
 * Interface for message serialization and deserialization.
 * Provides a unified mechanism for handling message conversions.
 */
public interface MessageSerializer {
    
    /**
     * Serializes a message object to JSON string.
     * 
     * @param message the message object to serialize
     * @return the JSON string representation of the message
     */
    String serialize(Object message);
    
    /**
     * Deserializes a JSON string to a message object.
     * 
     * @param json the JSON string to deserialize
     * @param messageId the ID of the message type
     * @return the deserialized message object
     */
    Object deserialize(String json, int messageId);
    
    /**
     * Serializes a message object to a byte array.
     * 
     * @param message the message object to serialize
     * @return the byte array representation of the message
     */
    default byte[] serializeToBytes(Object message) {
        String json = serialize(message);
        return json != null ? json.getBytes() : null;
    }
    
    /**
     * Deserializes a byte array to a message object.
     * 
     * @param bytes the byte array to deserialize
     * @param messageId the ID of the message type
     * @return the deserialized message object
     */
    default Object deserializeFromBytes(byte[] bytes, int messageId) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        String json = new String(bytes);
        return deserialize(json, messageId);
    }
}
